<?php


function generateQuestions ($questionArrayInformation) {
	
	$returnArray = array();
	$divNames = array();
	
		
	foreach ($questionArrayInformation as $key => $value) {
		
		$divHTML = generatePrimaryDiv($value);
		
		switch ($value['type']) {
			case 1: //drop down menu
				$returnArray[] = generateDropDownMenu($divHTML, $value, $divNames);
				break;
			case 2: //text input
				$returnArray[] = generateTextInput($divHTML, $value, $divNames);
				break;
			case 3: //date
				$returnArray[] = generateDateInput($divHTML, $value, $divNames);
				break;
			case 4: //medication
				$returnArray[] = generateMedicationDropDownMenu($divHTML, $value, $divNames);
				break;
			case 5: //medical test
				$returnArray[] = generateTestDropDown($divHTML, $value, $divNames);
				break;
		}
	}
	
	return $returnArray;

}


function generatePrimaryDiv ($questionArray) { //generates first div with question text
	
	$divHTML = '<div id="question_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '" class="questionDiv">';
	
	$divHTML .= '<div id="text_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '" class="questionText">';
			
	$divHTML .= $questionArray['text'];
		
	$divHTML .= '</div>';
		
	return $divHTML;
}




function generateDateInput($divHTML, $questionArray, $divNames) {
	
	
	//generate date div
	{
		$divHTML .= '<div id="dateInput_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '">';
		
		if (isset($_POST['datePick_type' . $questionArray['type'] . '_num' . $questionArray['qnumber']])) {
			$ans = $_POST['datePick_type' . $questionArray['type'] . '_num' . $questionArray['qnumber']];
			$divHTML .= '<input type="text" id="datePickid_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '" name="datePick_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . 
			'" value="' . $ans . '" style="text-align: center;">';
		} else {
			$divHTML .= '<input type="text" id="datePickid_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '" name="datePick_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '">';
		}
		
		
		$divHTML .= '<script type="text/javascript">
	
						$("#datePickid_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '").datepicker({ minDate: new Date(1950, 01, 01), showOn: "both", dateFormat: "yy-mm-dd"});
						
					</script>';
	}
	
	
	//adding end of fields
	{
		$divHTML .= '</div>'; //end of drop down div
		$divHTML .= '</div>'; //from function generatePrimaryDiv
	}
	
	
	//value to return
	{
		$returnArray = array();

		$returnArray[] = array("textId" => "text_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"answerName" => "datePick_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"type" => 3); // request name and select ID
		
		$returnArray[] = $divHTML;//return HTML to be rendered by webpage
			
	}
	
	return $returnArray;
}





function generateTextInput($divHTML, $questionArray, $divNames) {
	
	//text input
	{
		$divHTML .= '<div id="textInput_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '">';
		
		if (isset($_POST["textInputAns_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']])) {
			$ans = $_POST["textInputAns_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']];
		}
		
		if (isset($ans)) {
			$divHTML .= '<input type="text" class="textInput" name="textInputAns_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '" value="' . $ans . '">';
		} else {
			$divHTML .= '<input type="text" class="textInput" name="textInputAns_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '">';
		}
	}
	
	//adding end of fields
	{
		$divHTML .= '</select>'; //end of drop down menu
		$divHTML .= '</div>'; //end of drop down div
		$divHTML .= '</div>'; //from function generatePrimaryDiv
	}
	
	//value to return
	{
		$returnArray = array();

		$returnArray[] = array("textId" => "text_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"answerName" => "textInputAns_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"type" => 2); // request name and select ID
		
		$returnArray[] = $divHTML;//return HTML to be rendered by webpage
			
	}
	
	return $returnArray;
}






function generateDropDownMenu ($divHTML, $questionArray, $divNames) {
	
	//query
	{
		$conn = GetConnection();
		$sql = "SELECT * FROM answer_options WHERE id = '" . $questionArray['type_options'] . "' ORDER BY type";
		$sqlResults = $conn->query($sql);
	}
	
	//beginning dropdown elements
	{
		$divHTML .= '<div id="dropDown_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '">';
		$divHTML .= 	'<select name="Select_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '" style="margin-bottom: 10px; max-width: 200px;">';
		$divHTML .= 		'<option value="9999"> - Please Choose- </option>';
	}
	
	//fill select drop down
	while ($dropdownInfo = $sqlResults->fetch_assoc()) {
		
	if (isset($_POST["Select_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']]))	
		$ans = $_POST["Select_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']];
	
	if (isset($ans) && $ans == $dropdownInfo['type']) 
		{
			if (isset($dropdownInfo['description']))
				$divHTML .= '<option value="' . $dropdownInfo['type'] . '" selected="selected">' . $dropdownInfo['type'] . ': ' . $dropdownInfo['description'] . '</option>';
			else
			$divHTML .= '<option value="' . $dropdownInfo['type'] . '" selected="selected">' . $dropdownInfo['type'] . '</option>';
				
		} else {
			if (isset($dropdownInfo['description']))
				$divHTML .= '<option value="' . $dropdownInfo['type'] . '">' . $dropdownInfo['type'] . ': ' . $dropdownInfo['description'] . '</option>';
			else
				$divHTML .= '<option value="' . $dropdownInfo['type'] . '">' . $dropdownInfo['type'] . '</option>';
		}
	}
	
	
	//adding end of fields
	{
		$divHTML .= '</select>'; //end of drop down menu
		$divHTML .= '</div>'; //end of drop down div
		$divHTML .= '</div>'; //from function generatePrimaryDiv
	}
	
	
	//value to return
	{
		$returnArray = array();

		$returnArray[] = array("textId" => "text_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"answerName" => "Select_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"type" => 1); // request name and select ID
		
		$returnArray[] = $divHTML;//return HTML to be rendered by webpage
			
	}
	
	return $returnArray;
}




//generates drop down menu containing all medication names. Returns an array containing all text IDs and select names
function generateMedicationDropDownMenu ($divHTML, $questionArray, $divNames) {
	
	//query
	{
		$conn = GetConnection();	
		$sql = "SELECT * FROM Medication ORDER BY Name";
		$sqlResults = $conn->query($sql);
	}
	
	//beginning div and select elements
	{
		$divHTML .= '<div id="medDropDown_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '">';
		$divHTML .= 	'<select name="medSelect_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '" style="margin-bottom: 10px;">';
		$divHTML .= 		'<option value="9999"> - Treatment Name - </option>';
	}
	
	
	//fill select drop down
	while($medicationInfo = $sqlResults->fetch_assoc()) {
		
	if (isset($_POST["medSelect_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']])) {
		$ans = $_POST["medSelect_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']];
	}	
	 
	if (isset($ans) && $ans == $medicationInfo['ID']) 
		{
			$divHTML .= '<option value="' . $medicationInfo['ID'] . '" selected="selected">' . $medicationInfo['Name'] . '</option>';
				
		} else {
			
			$divHTML .= '<option value="' . $medicationInfo['ID'] . '">' . $medicationInfo['Name'] . '</option>';
		}
	}
	
	//adding end of fields
	{
		$divHTML .= '</select>'; //end of drop down menu
		$divHTML .= '</div>'; //end of drop down div
		$divHTML .= '</div>'; //from function generatePrimaryDiv
	}
	
	
	//value to return
	{
		$returnArray = array();

		$returnArray[] = array("textId" => "text_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"answerName" => "medSelect_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"type" => 4); // request name and select ID
		
		$returnArray[] = $divHTML;//return HTML to be rendered by webpage
			
	}
	
	return $returnArray;
}


function generateTestDropDown($divHTML, $questionArray, $divNames) {
	
	//query
	{
		$conn = GetConnection();	
		$sql = "SELECT * FROM Test ORDER BY ID";
		$sqlResults = $conn->query($sql);
	}
	
	//beginning div and select elements
	{
		$divHTML .= '<div id="testDropDown_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '">';
		$divHTML .= 	'<select name="testSelect_type' . $questionArray['type'] . '_num' . $questionArray['qnumber'] . '" style="margin-bottom: 10px;">';
		$divHTML .= 		'<option value="9999"> - Test Name - </option>';
	}
	
	
	//fill select drop down
	while($testInfo = $sqlResults->fetch_assoc()) {
		
	if (isset($_POST["testSelect_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']])) {
		$ans = $_POST["testSelect_type" . $questionArray['type'] . "_num" . $questionArray['qnumber']];
	}	
	 
	if (isset($ans) && $ans == $testInfo['ID']) 
		{
			$divHTML .= '<option value="' . $testInfo['ID'] . '" selected="selected">' . $testInfo['Name'] . '</option>';
				
		} else {
			
			$divHTML .= '<option value="' . $testInfo['ID'] . '">' . $testInfo['Name'] . '</option>';
		}
	}
	
	//adding end of fields
	{
		$divHTML .= '</select>'; //end of drop down menu
		$divHTML .= '</div>'; //end of drop down div
		$divHTML .= '</div>'; //from function generatePrimaryDiv
	}
	
	
	//value to return
	{
		$returnArray = array();

		$returnArray[] = array("textId" => "text_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"answerName" => "testSelect_type" . $questionArray['type'] . "_num" . $questionArray['qnumber'], 
		"type" => 4); // request name and select ID
		
		$returnArray[] = $divHTML;//return HTML to be rendered by webpage
			
	}
	
	return $returnArray;
}